package com.cwj.mansysback.dao.impl;

import com.cwj.mansysback.dao.BaseDao;
import com.cwj.mansysback.dao.api.TeacherDao;
import com.cwj.mansysback.entity.Account;
import com.cwj.mansysback.entity.Pack;
import com.cwj.mansysback.entity.Teacher;

import java.util.List;

public class TeacherDaoImpl extends BaseDao<Teacher> implements TeacherDao {

    private final static String table_name="teacher";
    private final static String other = "Account";


    @Override
    public boolean addTeacher(String tid, String tname, Double tsalary, String rid,Integer tage,boolean tsex) {

        String sql = "insert into "+TeacherDaoImpl.table_name+" (tid,tname,tsalary,rid,tage,tsex) values (?,?,?,?,?,?)";

        return super.update(sql,tid,tname,tsalary,rid,tage,tsex) > 0;
    }

    @Override
    public boolean deleteTeacher(String tid) {

        String sql = "delete from  "+TeacherDaoImpl.table_name+" where tid = ?";

        return super.update(sql,tid) > 0;
    }

    @Override
    public Teacher selectTeacher(String tid) {
//        String sql = "select tid as aid ,tname,tsalary,rid "+TeacherDaoImpl.table_name+","+ TeacherDaoImpl.other+" where tid = ?";
         String sql ="select tc.`rid` as rid,tc.`tname` as tname,tc.`tsalary`  as tsalary ,ac.`accrank` as accrank ,ac.`aid` as aid,ac.`avater` as avater,ac.`pawd` as pawd\n" +
                 "from teacher as tc left join account as ac on tc.`tid` = ac.`aid` where ac.`accrank` = 2 and ac.aid = ?";
        return super.select(Teacher.class,sql,tid);
    }

    @Override
    public List<Teacher> selectTeacher(int start, int sum, String field,boolean isAse) {

        String sql = "SELECT tc.*,ac.* FROM teacher AS tc LEFT JOIN account AS ac ON tc.`tid` " +
                "\t= ac.`aid` WHERE ac.`accrank` = 2  ORDER BY ?\t"+(isAse?"ASC":"DESC")+"\t LIMIT ? , ?";
        System.out.println("TeacherDaoImol-selectTeacher-sql:"+sql);

        int temp = Math.max(sum, 0);

        System.out.println(start+"\t"+sum+"\t"+(isAse?"ASC":"DESC")+"\t"+field);
        return super.selectBatch(Teacher.class,sql,field,(start-1)*temp,sum);
    }

    @Override
    public boolean updateTeacher(String tid, String tname, Double tsalary, String rid,Boolean tsex,Integer tage) {

        String sql = "update "+TeacherDaoImpl.table_name+" set tname = ? , tsalary = ?, rid = ?,tsex = ?,tage = ?  where tid =?";
        System.out.println(sql);

        return super.update(sql,tname,tsalary,rid,tsex,tage,tid) > 0;
    }

    @Override
    public Pack selectAllPack() {
        return BaseDao.selectAllPack(Account.getTableName(),Teacher.getRANK());
    }


}
